<template>
	<view>
		<view class="add-edit-charge">
			<text style="margin-left: 8px;font-size: 40rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 500;
			color: #333333;">
				{{equipmentOperatorType===1? "编辑":"添加"}}充电桩
			</text>
			<view class="equipment" style="margin-top: 32rpx;">
				<view class="title">
					<text style="color: red;">*</text>
					<text>设备编号</text>
				</view>
				<view class="input"  @click="scanSweep">
					<input :style="{color:equipmentOperatorType===1? '#999999':''}" v-model="equipmentCode" disabled="disabled"  type="text" placeholder="请扫码">
					<img :src="sweep" alt="">
				</view>
			</view>
			<view class="equipment" style="margin-top: 16rpx;">
				<view class="title">
					<text style="color: red;">*</text>
					<text>设备名称</text>
				</view>
				<view class="input">
					<input maxlength="15" v-model="equipmentName" type="text" placeholder="请输入">
				</view>
			</view>
		</view>
		<view class="btn" @click="postbindEquipment">
			<view>
				保存
			</view>
		</view>
	</view>
</template>

<script>
import sweep from "../../../static/sweep.png"
import {getUrlParam} from "../../../utils/index.js"
import { bindEquipment, socketDetail } from "./indexServ.js"
	export default {
		name: 'template',
		onLoad(option) {
			if(option.socketCode){
				this.socketCode = option.socketCode
				this.postSocketDetail()
				this.equipmentOperatorType = 1
			}
		},
		mounted() {
			
		},
		data(){
			return {
				sweep,
				socketCode: '',
				equipmentCode:'',
				equipmentName:'',
				equipmentOperatorType:'0',
			}
		},
		methods: {
			async postSocketDetail() {
				await socketDetail({
					socketCode: this.socketCode,
				}).then((res) => {
					const { code, body, message } = res
					if(code === "0000000") {
						this.equipmentCode = body.equipmentCode
						this.equipmentName = body.socketName
						return
					}
					this.$u.toast(message)
				}).catch(() => {
					this.$u.toast('接口返回异常')
				})
			},	
			scanSweep(){
				if(this.equipmentOperatorType===1){
					return
				}
				const self = this
				uni.scanCode({
					success:function(res){
						if(res.result.indexOf("mini.leyaoyao.com/gw/car-charging/family/qrcode?socketCode=") > -1 ){
							let param = res.result.split("socketCode=")
							let str = param[param.length-1].split("&")
							self.equipmentCode = str[0].slice(0, -2)
						}else{
							self.$u.toast("错误的设备编号")
						}
						
					}
				})
			},
			postbindEquipment(){
				if(this.equipmentCode===''){
					this.$u.toast("请填写设备编号")
					return
				}else if(this.equipmentName===''){
					this.$u.toast("请填写设备名称")
					return
				}
				bindEquipment({
					equipmentCode:this.equipmentCode,
					equipmentName:this.equipmentName,
					equipmentOperatorType:this.equipmentOperatorType
				}).then((res)=>{
					if(res.code==="0000000"){
						this.$u.toast("保存成功")
						// 跳转tabBar页面仅支持基于switchTab
						uni.switchTab({
							url: `/pages/chargeList/index`,
							// success: function () {
							// 	// 兼容switchTab跳转页面不重新载入
							//     var page = getCurrentPages().pop();
							//     if (page == undefined || page == null) return;
							//     page.onLoad();
							// },
							fail: error => {
								console.log('error；', error)
							}
						})
					}else{
						this.$u.toast(res.message)
					}
				})
			},
		}
	}
</script>

<style  lang="scss" scoped>
.add-edit-charge{
	width: 100vw;
	height: 100vh;
	background-color: #F5F5F5;
	padding: 24*2rpx 13*2rpx 0 16*2rpx;
	.equipment{
		width: 100%;
		background: #FFFFFF;
		border-radius: 8*2rpx;
		padding: 8*2rpx 16*2rpx 17*2rpx 12*2rpx;
		.title{
			font-size: 14*2rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #333333;
			margin-bottom: 16*2rpx;
		}
		.input{
			height: 22*2rpx;
			font-size: 16*2rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			position: relative;
			img{
				width: 20*2rpx;
				height: 20*2rpx;
				position: absolute;
				right: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}
	
}
.btn{
		width: 100%;
		height: 94*2rpx;
		background: #FFFFFF;
		position: absolute;
		padding: 0 12*2rpx;
		bottom: 0;
		padding-top: 8*2rpx;
		view{
			width: 100%;
			height: 44*2rpx;
			background: #1A8DFF;
			border-radius: 8*2rpx;
			font-size: 16*2rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 44*2rpx;
			text-align: center;
		}
	}
</style>